Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic.Compatibility

<System.Runtime.InteropServices.ProgId("SrmConector_NET.SrmConector")> Public Class SRM
    Private Declare Function Srmw32 Lib "c:\bin\f\Srmw32.dll" (ByVal Nodo As String, ByVal Apli As String, ByVal msg As String, ByRef Largo As Integer, ByVal Status As String, ByVal funcion As String, ByVal Contexto As String, ByVal Control As String) As Short

    Private m_ActivarLog As Boolean
    Private m_ArchivoLog As String

    Private m_RetornoAplicacion As String
    Private m_RetornoSrm As Integer
    Private m_StatusSrm As Short
    Private m_Respuesta As String

    Private m_Mensaje As String = "b12256781234W"
    Private m_Aplicacion As String
    Private m_Nodo As String
    Private m_Tipo As String
    Private m_Contexto As String
    Private m_Error As m_ErrorSRM
    Private m_Inicio As Date
    Private m_Fin As Date


    Public MasDatos As String = "N"
    Public m_SiguienteRegistro As String

    Public Sumando As Long = 1

    Public WriteOnly Property SiguienteRegistro() As String
        Set(ByVal Value As String)
            m_SiguienteRegistro = Trim(Value)        
        End Set
    End Property

    Public Structure m_ErrorSRM
        Dim m_ErrorCod As Integer
        Dim m_ErrorMsj As String

        Public Sub New(ByVal Codigo As Integer, ByVal Mensaje As String)
            Me.m_ErrorCod = Codigo
            Me.m_ErrorMsj = Mensaje
        End Sub
    End Structure

    Public ReadOnly Property Tiempo() As Long
        Get
            Return DateDiff(DateInterval.Second, m_Inicio, m_Fin)
        End Get
    End Property

    Public ReadOnly Property ErrorSRM() As m_ErrorSRM
        Get
            Return m_Error
        End Get
    End Property

    Public Function Invocar() As Boolean
        Try
            Dim strNowInicio As String
            Dim strResponse As New VB6.FixedLengthString(8166)
            Dim strStatus As New VB6.FixedLengthString(2)
            Dim strTipo As New VB6.FixedLengthString(2) ' Funcion.
            Dim strContexto As New VB6.FixedLengthString(2) ' Contexto.
            Dim strControl As String ' Control.
            Dim lngLength As Integer ' Largo del Mensaje.
            Dim NF As Short
            Dim qResp As Integer = 0

            strNowInicio = Format(Now, "yyyyMMddhhmmss")

            strStatus.Value = "00"
            strControl = Space(10000)

            If Trim(m_Contexto) = "" Then
                strContexto.Value = "00"
            Else
                strContexto.Value = Trim(m_Contexto)
            End If

            If Trim(m_Tipo) = "" Then
                strTipo.Value = "00"
            Else
                strTipo.Value = Trim(m_Tipo)
            End If

            strResponse.Value = m_Mensaje & Chr(0)
            lngLength = Len(m_Mensaje)

            m_Inicio = Now
            m_RetornoSrm = Srmw32(m_Nodo, m_Aplicacion, strResponse.Value, lngLength, strStatus.Value, strTipo.Value, strContexto.Value, strControl)
            m_Fin = Now

            m_Mensaje = "b12256781234W"

            m_Respuesta = Mid(strResponse.Value, 15, lngLength)

            MasDatos = Mid(strResponse.Value, 3, 1)

            Sumando = Sumando + CLng(Mid(strResponse.Value, 4, 7))
            m_SiguienteRegistro = Sumando.ToString.PadLeft(7, "0")


            m_StatusSrm = Val(strStatus.Value)

            If lngLength >= 2 Then
                m_RetornoAplicacion = Mid(m_Respuesta(qResp), 1, 2)
            End If

            'If m_ActivarLog Then
            '    NF = FreeFile()
            '    FileOpen(NF, m_ArchivoLog, OpenMode.Append)

            '    Print(NF, strNowInicio)
            '    Print(NF, VB6.Format(Now, "YYYYMMDDHHMMSS"))
            '    Print(NF, m_Nodo)
            '    Print(NF, m_Aplicacion)
            '    Print(NF, strTipo.Value & Space(2 - Len(strTipo.Value)))
            '    Print(NF, VB6.Format(Len(m_Mensaje), "0000"))
            '    Print(NF, m_Mensaje)
            '    Print(NF, VB6.Format(m_RetornoSrm, "0000"))
            '    Print(NF, VB6.Format(Val(strStatus.Value), "0000"))
            '    Print(NF, VB6.Format(Len(m_Respuesta), "0000"))
            '    PrintLine(NF, m_Respuesta)

            '    FileClose(NF)
            'End If
        Catch ex As Exception
            MsgBox("error " + Err.Description)
        Finally
            If m_RetornoSrm = 0 Then
                If IsNumeric(m_RetornoAplicacion) Then
                    If Val(m_RetornoAplicacion) = 0 Then
                        Invocar = True
                    Else
                        m_Error = New m_ErrorSRM(m_RetornoAplicacion, "")
                        Invocar = False
                    End If
                Else
                    Invocar = False
                End If
            Else
                Invocar = False
            End If
        End Try
    End Function


    Public Property Nodo() As String
        Get
            Return m_Nodo
        End Get
        Set(ByVal Value As String)
            m_Nodo = Trim(Value)
        End Set
    End Property

    Public Property Aplicacion() As String
        Get
            Return m_Aplicacion
        End Get
        Set(ByVal Value As String)
            m_Aplicacion = Trim(Value)
        End Set
    End Property

    Public Property Mensaje() As String
        Get
            Return m_Mensaje
        End Get
        Set(ByVal Value As String)
            m_Mensaje = m_Mensaje & m_SiguienteRegistro & MasDatos
            m_Mensaje = m_Mensaje & Value
        End Set
    End Property

    Public Property Tipo() As String
        Get
            Return m_Tipo
        End Get
        Set(ByVal Value As String)
            m_Tipo = Value
        End Set
    End Property

    Public Property Contexto() As String
        Get
            Return m_Contexto
        End Get
        Set(ByVal Value As String)
            m_Contexto = Value
        End Set
    End Property

    'Definicion de propiedades de solo lectura
    Public ReadOnly Property Respuesta() As String
        Get
            Return m_Respuesta
        End Get
    End Property

    Public Property StatusSrm() As Short
        Get
            StatusSrm = m_StatusSrm
        End Get
        Set(ByVal Value As Short)
        End Set
    End Property

    Public Property RetornoSrm() As Short
        Get
            RetornoSrm = m_RetornoSrm
        End Get
        Set(ByVal Value As Short)
        End Set
    End Property

    Public ReadOnly Property RetornoAplicacion() As String
        Get
            Return m_RetornoAplicacion
        End Get
    End Property

    Public Property ActivarLog() As Boolean
        Get
            Return m_ActivarLog
        End Get
        Set(ByVal Value As Boolean)
            m_ActivarLog = Value
        End Set
    End Property

    Public Property ArchivoLog() As String
        Get
            ArchivoLog = m_ArchivoLog
        End Get
        Set(ByVal Value As String)
            m_ArchivoLog = Value
        End Set
    End Property

    Public Sub New()
        'Dim strActivarLog As String

        'strActivarLog = GetConfiguracion("SrmConector", "ActivarLog")
        'm_ArchivoLog = GetConfiguracion("SrmConector", "ArchivoLog")

        'If UCase(Trim(strActivarLog)) = "SI" Then
        '    m_ActivarLog = True
        'Else
        '    m_ActivarLog = False
        'End If

    End Sub
End Class